<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>设备维保记录</title>
</head>

<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="../app/index.do"></a>
			</div>
			<div class="header-title">设备维保记录列表</div>
		</div>

	</div>
	<div class="table-container" data-options="region:'center'">
		<div>
			<table id="pro_table" class="easyui-datagrid" style="min-height: 100%">
			</table>
		</div>
	</div>
	<div id="pro_tb" class="search-show">
		<div class="search-input">
		<span>项目：</span>
		<span>
			<input style="width: 100%;" id="projectitemid" name="projectitemid" class="easyui-combobox"
				editable="false"
				data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: '../projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
								    if(data.length>0){
								    	$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },onSelect: function(rec){
							    	projectitemid = rec.id;
							    	loadData();
   									var url2='../equipments.do?projectitemid='+rec.id;
   									$('#equipment').combobox('reload', url2);
							    },panelHeight: 'auto'">
							    </span>
		</div>
		<div class="search-input text-right">
			<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
		</div>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 98%; max-height: 80%; padding: 5px; text-align: center;"></div>
	</div>
</body>
<script>
	var currentPro;
	var projectitemid;
	$(document).ready(function() {
		$('#isopen').checkbox({
			onChange : function(r) {
				if (r){
					document.getElementById("tr1").style.display = "";
					document.getElementById("tr2").style.display = "";
				}else{
					document.getElementById("tr1").style.display = "none";
					document.getElementById("tr2").style.display = "none";
				}
			}
		})
		$('#isdaily').checkbox({
			onChange : function(r) {
				if (r){
					document.getElementById("tr3").style.display = "";
				}else{
					document.getElementById("tr3").style.display = "none";
				}
			}
		})
	})
	function loadData() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "../equipmentMarkList",
			method : 'get',
			queryParams : {
				projectitemid : projectitemid
			},
			onSelect : function(index,row) {
				currentPro = row;
			},
			columns : [ [ {
				field : 'equipmentid',
				title : '设备名',
				width : '100%',
				formatter : function(value,row,index) {
					var html = '';
					html += '<div class="info-unreviewed">'
					if (row.equipment){
						html += '<div class="info-item-content" style="width:'+ ellipsis() +'px;margin-bottom:10px;">'+ row.equipment.name +'</div>'
					}
					if (row.starttime){
						html += '<div class="info-time pr90"><span>开机时间：'+ formatDate(row.starttime) +'</span></div>'
						html += '<div class="info-time pr90"><span>开机时长：'+ row.intervaltime +'小时</span></div>'
					}
					if(row.remark){
						html += '<div class="info-content" style="width:'+ ellipsis2() +'px;">'+ row.remark +'</div>'
					}
					if (row.isdaily == 1){
						html += '<div class="info-status green">已维保</div>'
						html += '<div class="info-time text-right"><span>维保时间：'+ formatDate(row.createtime) +'</span></div>'
					}else{
						html += '<div class="info-status refuse">未维保</div>'
					}
					if (row.attachments.length > 0){
						html+= '<div class="info-handle"><a class="d-btn btn-blue" onclick="viewAtt(' + index + ');">查看附件</a></div>'
					}
					html += '<div class="edit"><span class="info-edit" onclick="updatepro('+index+')"></span><span class="info-delete" onclick="deletepro('+index+')"></span></div></div>';
					return html;
				}
			} ] ]
		});
	}
	function ellipsis(){
		var WIDTH = $("body").width();
		WIDTH-=10;
		var width;
		width = WIDTH-60
		return width;
	}
	function ellipsis2(){
		var WIDTH = $("body").width();
		var width;
		width = WIDTH-10
		return width;
	}	
	function refreshProTable() {
		$('#pro_table').datagrid("load", {
			projectitemid : projectitemid
		});
	}
	function newpro() {
		window.location.href = "../app/equipmentmarkadd.do";
	}
	function updatepro(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		window.location.href = "../app/equipmentmarkadd.do?id=" + item.id;
	}
	function deletepro(index) {
		currentPro = $("#pro_table").datagrid('getRows')[index];
		if (currentPro){
			$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
				if (data){
					$.ajax({
						type : 'POST',
						url : "../equipmentMarkDelete.do?id=" + currentPro.id,
						contentType : "application/json",
						success : function(data) {
							if (data.success){
								$.messager.show({
									title : '删除',
									msg : '删除成功',
									timeout : 5000,
									showType : 'slide'
								});
								refreshProTable();
							}else{
								$.messager.alert('删除', '删除失败', 'error');
							}
						},
						dataType : "json"
					});
				}else{
				}
			});
		}else{
			$.messager.alert('更新', '请选择要删除的设备维保记录', 'warning');
		}
	}
	function viewAtt(index) {
		var row = $('#pro_table').datagrid('getRows')[index];
		var tb = "";
		$.each(row.attachments,function(i,val) {
			if(isImage(val.url)){
				tb+= '<img onclick="window.open(\'../'+val.url+ 
				'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
				'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
			}else{
				tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
				'\')"><span>'+val.name+'</span></a><br>';
			}
		});
		$('#displayAtt').empty().html(tb);
		$('#displayAtt').dialog({
			title : '附件',
			resizable : true,
			closed : false,
			cache : false,
			modal : true
		});
	}
	function attachmentFile(id,name) {
		$.get('../attachmentFile.do?id=' + id, function(data) {
			blob = new Blob([ data ])
			var a = document.createElement('a');
			a.download = name;
			a.href = window.URL.createObjectURL(blob)
			a.click()
		})
	}
	function remove(id) {
		$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
			if (data){
				$.ajax({
					type : 'POST',
					url : "../attachmentDelete.do?id=" + id,
					contentType : "application/json",
					success : function(data) {
						if (data.success){
							$("#att" + id).remove();
							$.messager.alert('删除附件', '删除附件成功', 'success');
						}else{
							$.messager.alert('删除附件', '删除附件失败', 'error');
						}
						window.setInterval("location.reload()", 2000);
					},
					dataType : "json"
				});
			}
		});
	}
	function isImage(url) {
		var index= url.lastIndexOf(".");
		var ext = url.substr(index+1);
		return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ,'ico'].
				indexOf(ext.toLowerCase()) !== -1;
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear();
		var month = date.getMonth() + 1; //月份是从0开始的
		if (month < 10){
			month = "0" + month;
		}
		var day = date.getDate();
		if (day < 10){
			day = "0" + day;
		}
		var hour = date.getHours();
		if (hour < 10){
			hour = "0" + hour;
		}
		var min = date.getMinutes();
		if (min < 10){
			min = "0" + min;
		}
		var sec = date.getSeconds();
		if (sec < 10){
			sec = "0" + sec;
		}
		var newTime = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
		return newTime;
	}
</script>
</html>